var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    orderId: 0,
    actualPrice: 0.00
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.orderId,
      actualPrice: options.actualPrice
    })
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  //向服务请求支付参数
  requestPayParam(e) {
    let that = this;
    util.request(api.PayPrepayId, { orderId: that.data.orderId, payType: 1 }).then(function (res) {
      if (res.errno === 0) {
        let payParam = res.data;

        //模板消息参数
        var self = this;
        let method = 'POST';

        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.timeStamp,
          'package': payParam.nonceStr,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            //发送模板消息
              let data1={
                "keyword1": {
                  "value": payParam.orderInfo.order_sn,
                  "color": "#173177"
                },
                "keyword2": {
                  "value": payParam.body,
                  "color": "#173177"
                },
                "keyword3": {
                  "value": payParam.orderInfo.add_time,
                  "color": "#173177"
                },
                "keyword4": {
                  "value": payParam.orderInfo.order_price,
                  "color": "#173177"
                },
                "keyword5": {
                  "value": "其他原因",
                  "color": "#173177"
                }
              };
            util.request(api.sendTemplateMsg, {
              openId: app.globalData.openId, formId: e.detail.formId, templateId: 'kc-XnjwLydSo_9k4ZdyBZukdIbBqTv6uD0dCLOMJYMI', data: data1
            }, "POST", 'application/json').then(function (res) {
              if (res.errno === 0) {
                console.log("success");
              }
              else {
                console.log("fail");
              }
            })
            //发送短信
            util.request(api.PaySuccess, { orderSn: payParam.orderInfo.order_sn, phone: payParam.orderInfo.mobile }, "POST", 'application/json')
              .then(function (res) {
                console.log(res);
              });
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=true',
            })
          },
          'fail': function (res) {
            //发送模板消息
              let data2={
                "keyword1": {
                  "value": payParam.orderInfo.order_sn,
                  "color": "#173177"
                },
                "keyword2": {
                  "value": payParam.orderInfo.add_time,
                  "color": "#173177"
                },
                "keyword3": {
                  "value": payParam.body,
                  "color": "#173177"
                },
                "keyword4": {
                  "value": payParam.orderInfo.order_price,
                  "color": "#173177"
                },
                "keyword5": {
                  "value": "网络等原因",
                  "color": "#173177"
                }
              };
            util.request(api.sendTemplateMsg, {
              openId: app.globalData.openId, formId: e.detail.formId, templateId: 'zfgNS-RQy_oWYHcg7daGgZSg5nHyk4zEcv3h7z4DVLY', data: data2
            }, "POST", 'application/json').then(function (res) {
              if (res.errno === 0) {
              console.log("success");
              }
              else{
                console.log("fail");
              }
            })

            wx.redirectTo({
              url: '/pages/payResult/payResult?status=false',
            })
          }
        })
      }
    });
  },
  startPay() {
    this.requestPayParam();
  }
})